package com.zjw.zy.utils

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.*
import android.util.Log
import com.zjw.zy.ext.longToast

/**
 * @author     ：zhong.jw
 * @date       ：Created in 2022/10/31 17:09
 * @description：
 */
object PhoneUtils {

    private const val TAG = "PhoneUtils"

    /**
     * 呼叫电话
     * @param telephone:telephone number
     * */
    fun call(telephone: String) {
        Intent(Intent.ACTION_DIAL).apply {
            data = Uri.parse("tel:${telephone}")
            try {
                AppUtils.getMainActivity().startActivity(this)
            } catch (e: Exception) {
                "此手机没有通信功能".longToast()
            }
        }
    }

    /**
     * 手机震动
     * @param mill:time
     * @param strong:strong of vibrator[1~255] or default -1
     * */
    fun vibrator(mill: Long, strong: Int) {
        Log.d(TAG, "vibrator() called with: mill = $mill")
        if (Build.VERSION.SDK_INT >= 31) {
            AppUtils.getApp().getSystemService(VibratorManager::class.java).apply {
                this.vibrate(
                    CombinedVibration.createParallel(
                        VibrationEffect.createOneShot(
                            mill,
                            strong
                        )
                    )
                )
            }
            return
        }
        (AppUtils.getApp().getSystemService(Context.VIBRATOR_SERVICE) as? Vibrator)?.apply {
            this.vibrate(VibrationEffect.createOneShot(mill, strong))
        }
    }

}